<?php
/**
 * 会员中心
 * User: chen guang
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\Controller;
use think\Db;

class Member extends Common{
	
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}

    /**
     * 会员列表
     */
    public function lists(){
		$where['delete']=1;
		$status=input("status");
		if($status!=''){
			$where['status']=$status;
		}
		$type=input("type");
		if($type!=''){
			$where['type']=$type;
		}
		if(input("keyword")){
			$where['nickname|mobile']=array("like",'%'.input('keyword').'%');
		}
		if(input("export")){
			$this->export($where);exit;
		}
        $list = db("member")->where($where)->order("id desc")->paginate(8);
        $this->assign('list',$list);
		$this->assign('status',input("status"));
		$this->assign('type',input("type"));
		$this->assign('keyword',input("keyword"));
        return $this->fetch();
    }
	/**
	 * 导出excel格式
	 * @createtime 2015-08-31
	 * @param string $fileName 导出文件名字
	 * @param array $headArr excel表格标题
	 * @param array $data excel数据
	 * @return file
	 */
	function ImportExcel($fileName,$headArr = array(),$data = array()){
		vendor("Excel.PHPExcel");

		/*
        $fileName = "导入订单格式";
        $headArr = array("手机","流量","流量类型","订单备注");
        $data = array(array("15999999999","10M","红包","选填"),array("15999999999","1G","红包","选填"),array("15999999999","1G","自由充","选填"));
        */

		$fileName .= ".xlsx";
		//创建新的PHPExcel对象
		$objPHPExcel = new \PHPExcel();
		$objProps = $objPHPExcel->getProperties();

		//设置表头
		$key = ord("A");
		foreach($headArr as $v){
			$colum = chr($key);
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum.'1', $v);
			$key += 1;
		}

		$column = 2;
		$objActSheet = $objPHPExcel->getActiveSheet();
		foreach($data as $key => $rows){ //行写入
			$span = ord("A");
			foreach($rows as $keyName=>$value){// 列写入
				$j = chr($span);
				$objActSheet->setCellValue($j.$column, $value);
				$span++;
			}
			$column++;
		}

		$fileName = iconv("utf-8", "gb2312", $fileName);
		//重命名表
		$objPHPExcel->getActiveSheet()->setTitle('Simple');
		//设置活动单指数到第一个表,所以Excel打开这是第一个表
		$objPHPExcel->setActiveSheetIndex(0);
		//将输出重定向到一个客户端web浏览器(Excel2007)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-type: application/vnd.ms-excel');
		header("Content-Disposition: attachment; filename=\"$fileName\"");
		header('Cache-Control: max-age=0');
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		/*$filePath = '/tmp/' . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp";
        $objWriter->save($filePath);
        readfile($filePath);
        unlink($filePath);*/
		$objWriter->save('php://output'); //文件通过浏览器下载
		die;
	}

	/**
	 * 会员列表
	 */
	public function export($where)
	{
		//创建对象
		$PHPExcel = new \PHPExcel();
		$objProps = $PHPExcel->getProperties();
		$headArr = array("会员ID","会员昵称","手机号码","openid","注册日期","省份","城市","区/县","会员类型","会员积分",'成交金额','成交订单数');
		$list = db('member')
				->where($where)
				->select();
		$order=db("order");
		foreach($list as $k => $v){
			$data[$k][] = $v['id'];
			$data[$k][] = $v['nickname'];
			$data[$k][] = $v['mobile'];
			$data[$k][] = $v['openid'];
			$data[$k][] = date("Y-m-d H:i",$v['register_time']);
			$data[$k][] = $v['province'];
			$data[$k][] = $v['city'];
			$data[$k][] = $v['county'];
/*			$data[$k][] = $v['address'];*/
			if($v['type']=='1'){
				$data[$k][] ='银卡会员';
			}
			if($v['type']=='2'){
				$data[$k][] ='金卡会员';
			}
			if($v['type']=='3'){
				$data[$k][] ='铂金卡会员';
			}
			if($v['type']=='4'){
				$data[$k][] ='黑金卡会员';
			}
			$data[$k][] = $v['integral'];
			$data[$k][] =$order->where("uid=".$v['id']." and status=2")->sum("price");
			$data[$k][] =$order->where("uid=".$v['id']." and status=2")->count();
		}

		//设置表头
		$key = ord("A");
		foreach($headArr as $v){
			$colum = chr($key);
			$PHPExcel->setActiveSheetIndex(0)->setCellValue($colum.'1', $v);
			$key += 1;
		}

		$column = 2;
		$objActSheet = $PHPExcel->getActiveSheet();
		foreach($data as $key => $rows){ //行写入
			 $span = ord("A");
			foreach($rows as $keyName=>$value){// 列写入
				 $j = chr($span);
				$objActSheet->setCellValue($j.$column, $value);
				$span++;
			}
			$column++;
		}
  		$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//按照指定格式生成Excel文件，'Excel2007'表示生成2007版本的xlsx，
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件
		//header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件
		header('Content-Disposition: attachment;filename="会员列表.xlsx"');//告诉浏览器输出浏览器名称
		header('Cache-Control: max-age=0');//禁止缓存
		$PHPWriter->save("php://output");
	}

	protected function column_str($key)
	{
		$array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ', 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ');
		return $array[$key];
	}

	protected function column($key, $columnnum = 1)
	{
		return $this->column_str($key) . $columnnum;
	}
	/*
  * 设置黑名单
  */
	public function setblack(){
		$id = input('id/d');
		if(!in_array(input('status'),array(0,1))){
			$this->error("状态有误");
		}
		$data['status'] = input('status');
		$res=db("member")->where("id='$id'")->update($data);
		if($res) $this->redirect($_SERVER['HTTP_REFERER']);
		$this->error("操作失败");
	}
	
	/*
  * 变会员等级
  */
	public function grade(){
		$id = input('id/d');
		if(!in_array(input('status'),array(1,2,3,4))){
			echo -1;exit;
		}
		$data['type'] = input('status');
		$res=db("member")->where("id='$id'")->update($data);
		if($res){
			echo 1;exit;
		}else{
			echo -1;exit;
		}
	}
	
}